package com.study.context;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.SmartLifecycle;
import org.springframework.stereotype.Component;

/**
 * @Description SmartLifecycle生命周期
 * @Author v_yangliu
 * @Date 2022/1/23 16:42
 * @Version 1.0
 **/
@Slf4j
@Component
public class TarsServerStartLifecycle implements SmartLifecycle, ApplicationContextAware {
    private ApplicationContext applicationContext;
    @Override
    public boolean isAutoStartup() {
        return true;
    }

    @Override
    public void stop(Runnable runnable) {
        log.info("=========我在上下文结束执行======");
        runnable.run();
    }

    @Override
    public void start() {
        log.info("=======我在刷新上下文启动时执行=======");
        String appName=applicationContext.getApplicationName();
        log.info("当前获取的启动应用名为:{},启动profile为:{}",appName,applicationContext.getEnvironment().getActiveProfiles());
    }

    @Override
    public void stop() {
        log.info("=========我在上下文结束执行======");
    }

    @Override
    public boolean isRunning() {
        return false;
    }

    @Override
    public int getPhase() {
        return 0;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
    }
}
